using SimpleJSON;
using UnityEngine;

namespace Gj
{
    public class DeepIndexHelper : NodeHelper
    {
        public GameObject empty;
        public GameObject item;

        [SelectRef(UIKey.DATA_KEY)] public SelectKey indexKey;
        public int customIndex = -1;
        public BindDataType dataType;
        public NodeHelper helper;
        public NodeHelper[] helpers;

        protected override BindDataType bindDataType => BindDataType.List;

        protected override void BindData(JSONArray array)
        {
            index = !indexKey.IsEmpty() ? indexKey.Get(_data).AsInt : index;
            var _i = customIndex < 0 ? index : customIndex;
            if (_i < 0) return;
            if (array.Count > _i && array[_i] != null && !array[_i].IsNull)
            {
                if (item != null) item.SetActive(true);
                if (empty != null) empty.SetActive(false);
                switch (dataType)
                {
                    case BindDataType.String:
                        if (helper != null) helper.SetData(array[_i].Value);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(array[_i].Value);
                        }
                        break;
                    case BindDataType.Int:
                        if (helper != null) helper.SetData(array[_i].AsInt);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(array[_i].AsInt);
                        }
                        break;
                    case BindDataType.Float:
                        if (helper != null) helper.SetData(array[_i].AsFloat);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(array[_i].AsFloat);
                        }
                        break;
                    case BindDataType.Bool:
                        if (helper != null) helper.SetData(array[_i].AsBool);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(array[_i].AsBool);
                        }
                        break;
                    case BindDataType.Object:
                        if (helper != null) helper.SetData(array[_i].AsObject);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(array[_i].AsObject);
                        }
                        break;
                    case BindDataType.List:
                        if (helper != null) helper.SetData(array[_i].AsArray);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(array[_i].AsArray);
                        }
                        break;
                    case BindDataType.None:
                        SetChildData(array[_i].AsObject);
                        for (int i = 0; i < helpers.Length; i++)
                        {
                            helpers[i].SetData(array[_i].AsObject);
                        }
                        break;
                }
            }
            else
            {
                if (item != null) item.SetActive(false);
                if (empty != null) empty.SetActive(true);
            }
        }
    }
}